from PyStats.GeneralTools import mean,stdDev

'''
Created on 20.5.2011

@author: Martin Vegi Kysel
'''

# rounding factor
ROUNDING_FACTOR = 3


def NormalizeResponseRate(extracted_ImageItems):
    '''
    @summary:   returns the list of imageItems with normalized response rates based on the MeanStandardizedResponseRateFunction
                ignores test trials because of self reference
    @param extracted_ImageItems: a list of image items
    @return: the same list with normalized peckRates (numPecks) 
    '''
    
    # list with average response rate for each session    
    sessionAverageResponseRates = []
    
    currentSession = 1
    currentSum = 0.0
    currentTrial = 0
    
    for item in extracted_ImageItems:
        
        # ignore test trials for the acquisition of the basic response rate
        if item.itemClass==0:
            continue
        
        if item.session>currentSession:
            # new session
            currentSession=item.session
            
            # compute the average response rate
            currentSum/=currentTrial
            
            # write the average rate into the list
            sessionAverageResponseRates.append(currentSum)
            
            # reset the values
            currentSum=0.0
            currentTrial=0
            
        if item.trial>currentTrial:
            # new trial
            currentTrial+=1
            
            # add the number of responses to the summary
            currentSum+=item.numPecks
    
    
    # the last session
    currentSum/=currentTrial
    sessionAverageResponseRates.append(currentSum)
    
    
    # into the list again
    for item in extracted_ImageItems:
        
        # divide the response rate by the average of the session
        normalizedValue = round(item.numPecks/sessionAverageResponseRates[item.session-1],ROUNDING_FACTOR)
        
        # replace the rate
        item.numPecks=normalizedValue
        
    # return the normalized list
    return extracted_ImageItems
    
    
    
def computeMeanStandardizedResponseRate(normalized_imageItems, prefix=""):
    '''
    @summary: takes a list of normalized imageItems and computes their mean across all sessions
                can extract a given prefix or can work with a already-preselected list of image items
    @param normalized_imageItems: list of image items
    @param prefix: if given computes the MRR only for the given prefix
    @return: the MSRR for the given prefix, rounded to the NORMALIZATION_FACTOR, the standard deviation
    '''
    
    
    # initialization
    x = []
    
    
    for item in normalized_imageItems:
        
        # select test images that fit the selected prefix
        # no prefix can be given
        if item.itemClass==0 and item.image_name.find(prefix)!=-1:
            x.append(item.numPecks)
            
    prefixMean = mean(x)
    prefixStdDev = stdDev(x)
    
    return round(prefixMean,ROUNDING_FACTOR),prefixStdDev

def computeMeanStandardizedResponseRateForPrefixes(normalized_imageItems,prefixes):
    nPRforPrefixes = []
    
    for i,prefix in prefixes:
        nPRforPrefixes[i] = computeMeanStandardizedResponseRate(normalized_imageItems, prefix)
    
    return nPRforPrefixes   
    